package org.jeecg.modules.output.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.consumptionElectricity.entity.ElectricitySelectDTO;
import org.jeecg.modules.output.entity.Output;
import org.jeecg.modules.output.entity.OutputSelectDTO;
import org.jeecg.modules.output.mapper.OutputMapper;
import org.jeecg.modules.output.service.IOutputService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 产量
 * @Author: jeecg-boot
 * @Date:   2023-06-20
 * @Version: V1.0
 */
@Service
public class OutputServiceImpl extends ServiceImpl<OutputMapper, Output> implements IOutputService {

    @Override
    public List<Output> getByDept(OutputSelectDTO selectDTO) {
        LambdaQueryWrapper<Output> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Output::getDept, selectDTO.getDept());
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Output> selectByTime(ElectricitySelectDTO selectDTO) {
        if (selectDTO.getStartTime() == null || selectDTO.getEndTime() == null) {
            throw new JeecgBootException("服务器忙,请稍后再试...");
        }
        Date start = Date.from(selectDTO.getStartTime().toInstant());
        Date end = Date.from(selectDTO.getEndTime().toInstant());
        //log.info("起始时间: {}, 结束时间: {}", s, e);
        QueryWrapper<Output> wrapper = new QueryWrapper<>();
        wrapper.between("time", start, end);
        if (selectDTO.getDept() != null){
            wrapper.eq("dept", selectDTO.getDept());
        }
        if (selectDTO.getProduct() != null){
            wrapper.eq("product", selectDTO.getProduct());
        }
        if ("date".equals(selectDTO.getDatePickerType())) {
            wrapper.select("sum(output) output", "DATE_FORMAT(time,'%Y%m%d') days", "time")
                    .groupBy("days").orderByAsc("days");
        } else {
            wrapper.select("sum(output) output", "DATE_FORMAT(time,'%Y%m') months", "time")
                    .groupBy("months").orderByAsc("months");
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Output> selectCarbonEmission(ElectricitySelectDTO selectDTO) {
        if (selectDTO.getStartTime() == null || selectDTO.getEndTime() == null) {
            throw new JeecgBootException("服务器忙,请稍后再试...");
        }
        Date start = Date.from(selectDTO.getStartTime().toInstant());
        Date end = Date.from(selectDTO.getEndTime().toInstant());
        //log.info("起始时间: {}, 结束时间: {}", s, e);
        QueryWrapper<Output> wrapper = new QueryWrapper<>();
        wrapper.between("time", start, end);
        if ("date".equals(selectDTO.getDatePickerType())) {
            wrapper.select("sum(output) output", "DATE_FORMAT(time,'%Y%m%d') days", "product")
                    .groupBy("days", "product").orderByAsc("days");
        } else {
            wrapper.select("sum(output) output", "DATE_FORMAT(time,'%Y%m') months", "product")
                    .groupBy("months", "product").orderByAsc("months");
        }
        return baseMapper.selectList(wrapper);
    }

}
